var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
	try{
		console.log( 'index ==========' );
		console.log('redirectUrl', redirectUrl );
		console.log('ticket', ticket );
		console.log('mainUri', mainUri );
		console.log('shareImg', shareImg );
		console.log('appId', appId );
		console.log('hostName', hostName );

		res.render( 'index', { openid: '', redirectUri: redirectUrl, env: env ,ticket:ticket,mainUri:mainUri,shareImg:shareImg,appId:appId, hostName: hostName} );

	}catch (e){
		console.error( 'error', e.message );
	}
});


//test verification
router.get('/verification', function(req, res, next) {
	var _code = req.query.code || '';
	console.log( 'code ==========', _code );
	if( !!_code ){
		try{
			var tokenObj = WechatService.getAccessToken( _code );
				tokenObj.then( function(res){
					return res.json();
				}).then(function(json) {
			        console.log( 'getOpenId json', json);
					// getUserInfo(function(subscribe){
					// 	console.log( 'subscribe', subscribe);
					// 	if(subscribe){
							json.redirectUri = redirectUrl;
							json.env = env;
							json.hostName = hostName;
							json.ticket=ticket;
							json.mainUri=mainUri;
							json.shareImg=shareImg;
							json.appId=appId;
							// res.send( json );
							res.render( 'index', json );
						// }else {
						// 	res.redirect('https://mp.weixin.qq.com/mp/profile_ext?action=home&scene=110&__biz=MzI2NzEwMDAzMw==#wechat_redirect');
						// }
					// },access_token,json.openid);

			    });			
			}catch(e){
				console.error( 'error', e.message );
			}
	}else{
		// var _getCodeUrl = wechatConfig.getCodeUrl();
		// res.redirect( _getCodeUrl ); 
		// res.render( 'error' );
		res.render( 'index', { openid: '', redirectUri: redirectUrl, env: env ,ticket:ticket,mainUri:mainUri,shareImg:shareImg,appId:appId, hostName: hostName} );
	}
});

//get wechat openid by code 
router.get('/getOpenId', function(req, res, next){
	try{
	    var code = req.query.code || '';
	    if( code ){
			var tokenObj = WechatService.getAccessToken( code );
			tokenObj.then( function(res){
				return res.json();
			}).then(function(json) {
		        console.log( 'getOpenId json', json);
		        res.send( json ); 
		    });
	    }else{
	    	res.send({error:'code 不存在'});
	    }		
	}catch(e){
		console.log( 'e.message', e.message );
	}
});

//get wechat openid by code
router.post('/getUserInfo', function(req, res, next){
	var openId = req.body.openId;
	console.log( 'get usr info body', req.body );
	try{
		console.log( '++++openId', openId );
		request( WechatService.getRequestParams(wechatConfig.getUserInfo(access_token,openId)) , function( err, response, body ){
			console.log('getUserInfo+++++++02',body);
			res.send( body );
		}).on( 'error', function( e ){
		});
	}catch(e){
		console.log( 'e.message', e.message );
	}
});

function getJsapiTicket(access_token){
	try{
		request( WechatService.getRequestParams(wechatConfig.getJsapiTicket(access_token)) , function( err, response, body ){
			console.log('getJsapiTicket+++++++',body.ticket);
			ticket=body.ticket;
		}).on( 'error', function( e ){
		});
	}catch(e){
		console.log( 'e.message', e.message );
	}
}
function getCommonToken(){
	try{
		request( WechatService.getRequestParams(wechatConfig.getCommonToken()) , function( err, response, body ){
			console.log('getCommonToken+++++++',body);
			access_token=body.access_token;
			getJsapiTicket(body.access_token);
		}).on( 'error', function( e ){
		});
	}catch(e){
		console.log( 'e.message', e.message );
	}
}
function getUserInfo(callback,access_token,openid){
	try{
		request( WechatService.getRequestParams(wechatConfig.getUserInfo(access_token,openid)) , function( err, response, body ){
			console.log('getUserInfo+++++++01',body);
			callback.call(this,body.subscribe);
		}).on( 'error', function( e ){
		});
	}catch(e){
		console.log( 'e.message', e.message );
	}
}
module.exports = router;
